package com.JFapp.utils;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {
    //私有构造类，表明不能直接new出对象
    private BeanCopyUtils(){}

    //通过Class clazz字节码反射方式创建对象
    public static <V> V copyBean(Object source,Class<V> clazz) {
        //创建目标对象
        V result = null;
        try {
            result = clazz.newInstance();
            //实现属性copy
            BeanUtils.copyProperties(source, result);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回结果
        return result;
    }

    //使用Stream流转换数据
    public static <O,V> List copyBeanList(List<O> list, Class<V> clazz){
        return   list.stream()
                .map(o->copyBean(o,clazz))
                .collect(Collectors.toList());
    }
}
